package com.wly.ui
{
	import com.wly.ItemEditerMain;
	import com.wly.data.TypePool;
	import com.wly.data.TypeVO;
	
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	public class AddType extends Sprite
	{
		private var _tfId:TextField;
		private var _tfDesc:TextField;
		private var _btnAdd:SimpleButton;
		private var _btnCancel:SimpleButton;
		public var itemEditMain:ItemEditerMain;
		
		public function AddType()
		{
			super();
			initChilds();
		}
		
		private function initChilds():void
		{
			_tfId   = getChildByName("tfId") as TextField;
			_tfId.restrict = "0-9";
			_tfDesc = getChildByName("tfDesc") as TextField;
			_btnAdd = getChildByName("btnAdd") as SimpleButton;
			_btnAdd.addEventListener(MouseEvent.CLICK, addType);
			_btnCancel = getChildByName("btnCancel") as SimpleButton;
			_btnCancel.addEventListener(MouseEvent.CLICK, cancel);
			_tfId.text = TypePool.getInstance().getTypeList().length.toString();
		}
		
		private function addType(e:MouseEvent):void
		{
			var type:TypeVO =  new TypeVO();
			type.id   = parseInt(_tfId.text);
			type.desc = _tfDesc.text;
			TypePool.getInstance().getTypeList().push(type);
			this.parent.removeChild(this);
			
			if(itemEditMain)
			{
				itemEditMain.onTypesChange();
			}
		}
		
		private function cancel(e:MouseEvent):void
		{
			this.parent.removeChild(this);
		}
	}
}